package com.lc.advertising.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.advertising.R;
import com.lc.advertising.app.AppActivity;
import com.lc.advertising.http.request.WalletApi;
import com.lc.advertising.http.response.WalletBean;
import com.lc.advertising.other.IntentKey;
import com.lc.advertising.popup.ListPopup;
import com.lc.advertising.util.SPUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WalletActivity extends AppActivity {

    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private int mPage = 1;
    private BaseQuickAdapter adapter;
    private List<WalletBean.JiluBean> dataList = new ArrayList<>();
    private int total;
    private String type = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_wallet;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BaseQuickAdapter<WalletBean.JiluBean, BaseViewHolder>(R.layout.item_jiaoyi) {
            @Override
            protected void convert(@NotNull BaseViewHolder holder, WalletBean.JiluBean dataBean) {

                holder.setText(R.id.tv_name, dataBean.getLeixing());
                holder.setText(R.id.tv_money, dataBean.getMoney());
                holder.setText(R.id.tv_date, dataBean.getCreate_time());
                holder.setText(R.id.tv_staute, dataBean.getZhuangtai());

            }
        };
        View emptyview = View.inflate(this, R.layout.empty_emptylayout, null);
        adapter.setEmptyView(emptyview);
        recyclerview.setAdapter(adapter);
        wallet(mPage, type);
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(final @NonNull RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(() -> {
                    mPage++;
                    if (mPage < total) {
                        walletmore(mPage, type);

                        refreshLayout.finishLoadMore();
                    } else {
                        toast("数据全部加载完毕");
                        refreshLayout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
                    }
                }, 1000);
            }

            @Override
            public void onRefresh(final @NonNull RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(() -> {
                    mPage = 1;
                    wallet(mPage, type);
                    refreshLayout.finishRefresh();
                }, 1000);

            }
        });
    }

    private void wallet(int page, String type) {
        EasyHttp.post(this)
                .api(new WalletApi()
                        .setPage(page).setUid(SPUtil.get(this, IntentKey.UID, "")).setType(type))
                .request(new HttpCallback<WalletBean>(this) {

                    @Override
                    public void onSucceed(WalletBean data) {
                        if (data.getCode() == 200) {
                            total = data.getCount();
                            dataList.clear();
                            dataList.addAll(data.getJilu());
                            adapter.setNewData(dataList);
                            adapter.notifyDataSetChanged();
                        }

                    }


                });
    }

    private void walletmore(int page, String type) {
        EasyHttp.post(this)
                .api(new WalletApi()
                        .setPage(page).setUid(SPUtil.get(this, IntentKey.UID, "")).setType(type))
                .request(new HttpCallback<WalletBean>(this) {

                    @Override
                    public void onSucceed(WalletBean data) {
                        if (data.getCode() == 200) {
                            total = data.getCount();

                            dataList.addAll(data.getJilu());
                            adapter.setNewData(dataList);
                            adapter.notifyDataSetChanged();
                        }

                    }


                });
    }

    @OnClick({R.id.image_back, R.id.tv_chongzhi, R.id.tv_tixian, R.id.sb_type, R.id.tv_cart})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.image_back:
                finish();
                break;
            case R.id.tv_chongzhi:
                startActivity(RechargeActivity.class);
                break;
            case R.id.tv_tixian:
                startActivity(WithdrawalActivity.class);
                break;
            case R.id.sb_type:
                // 菜单弹窗
                new ListPopup.Builder(this)
                        .setList("全部", "奖励", "充值", "提现", "订单")
//                        .addOnShowListener(popupWindow -> toast("PopupWindow 显示了"))
//                        .addOnDismissListener(popupWindow -> toast("PopupWindow 销毁了"))
                        .setListener((ListPopup.OnListener<String>) (popupWindow, position, s) ->{
                            switch (s){
                                case "全部":
                                    type="";
                                    break;
                                case "奖励":
                                    type="1";
                                    break;
                                case "充值":
                                    type="2";
                                    break;
                                case "提现":
                                    type="3";
                                    break;
                                case "订单":
                                    type="4";
                                    break;
                            }
                             mPage=1;
                            wallet(mPage,type);
                                }

                           )
                        .showAsDropDown(view);
                break;
            case R.id.tv_cart:
                startActivity(BankCartActivity.class);

                break;

        }
    }
}
